package com.example.ktapplication
import android.app.Application
import android.content.Context
import androidx.multidex.MultiDex
import com.elvishew.xlog.LogConfiguration
import com.elvishew.xlog.LogLevel
import com.elvishew.xlog.XLog
import com.elvishew.xlog.flattener.DefaultFlattener
import com.elvishew.xlog.interceptor.BlacklistTagsFilterInterceptor
import com.elvishew.xlog.printer.AndroidPrinter
import com.elvishew.xlog.printer.file.FilePrinter
import com.elvishew.xlog.printer.file.backup.FileSizeBackupStrategy
import com.elvishew.xlog.printer.file.clean.FileLastModifiedCleanStrategy
import com.example.applicationtest.utils.LogBorderFormatter
import com.example.applicationtest.utils.LogFileNameGenerator
import com.example.applicationtest.utils.LogJsonFormatter
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshFooter
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.DefaultRefreshFooterCreator
import com.scwang.smart.refresh.layout.listener.DefaultRefreshHeaderCreator
import com.tencent.bugly.crashreport.CrashReport

class App : Application() {


    override fun onCreate() {
        super.onCreate()
        CrashReport.initCrashReport(this, "ea9e03d25d", true);
        AppGlobals.mApplication = this
        initLog()
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(object : DefaultRefreshHeaderCreator {
            override fun createRefreshHeader(context: Context, layout: RefreshLayout): RefreshHeader {
                return ClassicsHeader(context);
            }

        })
        SmartRefreshLayout.setDefaultRefreshFooterCreator(object : DefaultRefreshFooterCreator {
            override fun createRefreshFooter(context: Context, layout: RefreshLayout): RefreshFooter {
                return ClassicsFooter(context);
            }
        })
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this);
    }

    ///初始化日志
    private fun initLog() {
        val config = LogConfiguration.Builder()
            .enableBorder()
            .borderFormatter(LogBorderFormatter())
            .addInterceptor(BlacklistTagsFilterInterceptor("Log1", "Log2", "Log3"))
            .tag("HEFA")
            .jsonFormatter(LogJsonFormatter())
            .build()
        val androidPrinter = AndroidPrinter(true)
        val filePrinter = FilePrinter.Builder(getExternalFilesDir(null)!!.path)
            .fileNameGenerator(LogFileNameGenerator())
            .backupStrategy(FileSizeBackupStrategy((3 * 1024 * 1024).toLong()))
            .cleanStrategy(FileLastModifiedCleanStrategy(30L * 24L * 60L * 60L * 1000L))
            .flattener(DefaultFlattener())
            .build()
        XLog.init(LogLevel.ALL, config, filePrinter, androidPrinter)
    }
}